package ee.ut.aa.neuraltic.main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Scanner;

import org.apache.log4j.PropertyConfigurator;

import ee.ut.aa.neuraltic.logic.Competition;
import ee.ut.aa.neuraltic.model.Board;
import ee.ut.aa.neuraltic.model.HumanPlayer;
import ee.ut.aa.neuraltic.model.NeuralPlayer;
import ee.ut.aa.neuraltic.model.Player;
import ee.ut.aa.neuraltic.model.TicBoard;
import ee.ut.aa.neuraltic.neural.Layer;
import ee.ut.aa.neuraltic.neural.Neuron;
import ee.ut.aa.neuraltic.neural.Synaps;
import ee.ut.aa.neuraltic.neural.TicNetwork;

public class HumanTest {

	public static void main( String[] args ) throws FileNotFoundException, IOException {

		Properties logProperties = new Properties();

		// load our log4j properties / configuration file
		logProperties.load( new FileInputStream( "etc/log4j.properties" ) );
		PropertyConfigurator.configure( logProperties );
		
		InputStream io;

		try {
			io = new FileInputStream( args[0] );
		} catch( FileNotFoundException e ) {

			System.out.println( "File not found: " + args[0] );
			return;
		}

		Scanner in = new Scanner( io );
		Locale loc = new Locale("en", "US");
		
		in.useLocale(loc);
		in.useDelimiter( ";" );

		TicNetwork network = new TicNetwork();

		List<Layer> layers = network.getLayers();

		for( Layer layer : layers ) {
			for( Neuron neuron : layer.getNeurons() ) {
				for( Synaps synaps : neuron.getSynapses() ) {

					synaps.setWeight( in.nextDouble() );
				}
			}
		}

		Competition comp = new Competition();

		Player human = new HumanPlayer();
		human.setValue( -1 );

		comp.competition( new NeuralPlayer( Board.PLAYER_ONE, network ), human, new TicBoard() );

		human = new HumanPlayer();
		human.setValue( 1 );

		comp.competition( human, new NeuralPlayer( Board.PLAYER_TWO, network ), new TicBoard() );

	}
}
